package com.example.contestsystem.Dao;

import com.example.contestsystem.Entity.Team;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface TeamRepository extends JpaRepository<Team, Integer> {
    /**
     * 通过团队id返回团队对象
     */
    Team findByTeamId(String teamId);

    List<Team> findByContestId(String contestId);

    Team findTeamByContestId(String contestId);

    Team findByCaptainAccountAndContestId(int captainId, String contestId);


    @Modifying
    @Transactional
    void deleteByTeamId(String teamId);

    @Modifying
    @Transactional
    void deleteByCaptainAccountAndContestId(int captainId, String contestId);

    @Modifying
    @Transactional
    @Query("UPDATE Team t SET t.teamMemberNum = t.teamMemberNum + 1 WHERE t.teamId = :teamId")
    void incrementTeamMemberNum(@Param("teamId") String teamId);

    @Modifying
    @Transactional
    @Query("UPDATE Team t SET t.teamMemberNum = t.teamMemberNum - 1 WHERE t.teamId = :teamId")
    void decrementTeamMemberNum(@Param("teamId") String teamId);

    @Modifying
    @Transactional
    void deleteByContestId(String contestId);

}
